package com.xique.client.controller;

import com.xique.client.api.domain.ClientUserInfo;
import com.xique.client.api.model.LoginClientUser;
import com.xique.client.service.service.IClientUserInfoService;
import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shd
 * @create 2021/7/21 9:33
 */
@RestController
@RequestMapping("/user")
public class ClientUserInfoController extends BaseController {

    @Autowired
    private IClientUserInfoService clientUserInfoService;

    /**
     * 获取当前用户信息
     */
    @GetMapping("/info/{username}")
    @ApiOperation("登录时获取当前用户信息")
    public R<LoginClientUser> info(@PathVariable("username") String username)
    {
        ClientUserInfo user = clientUserInfoService.selectClientUserByLoginName(username);
        if (StringUtils.isNull(user))
        {
            return R.fail("用户名或密码错误");
        }
        //TODO 添加角色集合和权限集合
        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(sysUser.getUserId());
        // 权限集合
//        Set<String> permissions = permissionService.getMenuPermission(sysUser.getUserId());
        LoginClientUser loginClientUser = new LoginClientUser();
        loginClientUser.setClientUserInfo(user);
        return R.ok(loginClientUser);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    @ApiOperation("获取当前用户信息")
    public AjaxResult getInfo()
    {
        Long userId = SecurityUtils.getUserId();
        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(userId);
        // 权限集合
//        Set<String> permissions = permissionService.getMenuPermission(userId);
        Map<String, Object> map = new HashMap<>(3);
        map.put("user", clientUserInfoService.selectClientUserById(userId));
//        map.put("roles", roles);
//        map.put("permissions", permissions);
        return AjaxResult.success(map);
    }
}
